/**
 * 
 */
package vue.popup;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import model.solver.ImportESolver;
import model.solver.ImportSolverParameter;
import controller.IController;

/**
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGImportSolverDialog extends JDialog implements ActionListener {

	private static final long		serialVersionUID	= 1L;
	public static final String		TITLE				= "SOLVER IMPORTATION";
	public static final String		INFORMATION			= "Informations: \n-The executable file must be in the solveur directory.\n-The description field is used to identified your solver graphically.\n-Complete the parameter fields as the default parameters that will be automatically set.\n-Click on the parameter that supposed to be the CNF file to tell the application\n   to fill it with the CNF file adress\n-You must provide at least the first parameter (the executable file name).";
	public static final String		DESCRIPTION_TEXT	= "Description";
	public static final String		IMPORT_BUTTON		= "Import";


	public static final String		INFORMATION_TITLE	= "Informations";


	private IController				controller;

	private JButton					importbutton;
	private JPanel					panel;

	private JLabel					descritpionLabel;
	private JTextField				descritpionField;
	private JPanel					descriptionPanel;

	private ImportSolverParameter	param				= new ImportSolverParameter();
	private IGImportDialogParameter	paramDialog;

	/**
	 * Create {@link IGImportSolverDialog}.
	 * 
	 * @param controller
	 *            the {@link IController} of the ampplication
	 * @param owner
	 *            window owner of the dialog
	 */
	public IGImportSolverDialog(IController controller, Window owner) {
		super(owner, TITLE);

		this.controller = controller;

		panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		setContentPane(panel);

		// instruction
		panel.add(new JTextArea(INFORMATION));

		// information
		descriptionPanel = new JPanel(new GridLayout(1, 2));

		descritpionLabel = new JLabel(DESCRIPTION_TEXT);
		descriptionPanel.add(descritpionLabel);
		descritpionField = new JTextField("Imported solver");
		descriptionPanel.add(descritpionField);
		descriptionPanel.setBorder(BorderFactory.createTitledBorder(INFORMATION_TITLE));

		panel.add(descriptionPanel);

		// parametre
		paramDialog = new IGImportDialogParameter(param);
		JScrollPane pane = new JScrollPane(paramDialog);
		panel.add(pane);

		// bouton
		importbutton = new JButton(IMPORT_BUTTON);
		// run.setEnabled(false);
		importbutton.setAlignmentX(JButton.RIGHT_ALIGNMENT);
		importbutton.addActionListener(this);

		panel.add(importbutton);

		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		pack();
	}

	/**
	 * Add the solver to the application.
	 */
	private void addSolver() {
		ImportESolver solver = new ImportESolver(param.getParams().get(0), descritpionField.getText(), descritpionField.getText(), param);

		controller.addAvailableSolver(solver);
	}

	/**
	 * Check if the parameter are correct and update them in the parameter object.
	 * 
	 * @return if the parameter are ok.
	 */
	public boolean checkAndSetParameter() {

		boolean good = true;

		// check parametre
		good = paramDialog.checkAndSetParameter();

		// check description
		if (descritpionField.getText().isEmpty())
		{// pb description
			good = false;
			descriptionPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
		}
		else
			descriptionPanel.setBorder(null);

		return good;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == importbutton)
		{// demande d'importer
			if (checkAndSetParameter())
			{// peut lancer les param
				addSolver();
				dispose();
			}
			else
				pack();

			return;
		}
	}


}
